package com.tiancheng.onecode.trade.utils;

import com.tiancheng.onecode.trade.enums.OperateCodeEnum;
import com.tiancheng.onecode.utils.CommonUtil;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 订单编号生成
 *
 * @author kellen
 * @date 2024/2/28
 */
@Component
public class OrderIdHelper {

    @Resource
    private RedissonClient redissonClient;


    /**
     * 获取支付单号
     *
     * @param uid
     * @return
     */
    public String getOrderId(String idPre, String uid) {
        long id = (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() << 12) | Math.abs(uid.hashCode()) | CommonUtil.getRandom(10, 10000000);
        StringBuilder paymentId = new StringBuilder(idPre);
        return paymentId.append(OperateCodeEnum.order.getCode()).append(id).toString();
    }

    /**
     * 获取退款单号
     *
     * @param idPre 订单号前缀
     * @param uid   用户id
     * @return
     */
    public String getRefund(String idPre, String uid) {
        long id = (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() << 12) | Math.abs(uid.hashCode()) | CommonUtil.getRandom(10, 10000000);
        StringBuilder paymentId = new StringBuilder(idPre);
        return paymentId.append(OperateCodeEnum.refund.getCode()).append(id).toString();
    }

    /**
     * 订单号列表获取唯一编号
     *
     * @param uid
     * @param orderId
     * @param payWay
     * @return
     */
    public String getUniqueId(String uid, String orderId, String payWay, String payScene) {
        StringBuilder builder = new StringBuilder(uid);
        if (StringUtils.isNotEmpty(payScene)) {
            return builder.append("_").append(Math.abs(orderId.hashCode())).append("_").append(payWay).append("_").append(payScene.toLowerCase()).toString();
        } else {
            return builder.append("_").append(Math.abs(orderId.hashCode())).append("_").append(payWay.toLowerCase()).toString();
        }
    }


    /**
     * 订单号列表获取唯一编号
     *
     * @param uid
     * @param orderIds 订单号集合
     * @param payWay
     * @return
     */
    public String getUniqueIdByOrderIds(String uid, List<String> orderIds, String payWay, String payScene, String batchId) {
        StringBuilder builder = new StringBuilder(uid);
        if (CollectionUtils.isNotEmpty(orderIds)) {
            Collections.sort(orderIds);
        }
        final String orderIdsString = StringUtils.isEmpty(batchId) ? String.join("", orderIds) : batchId;
        if (StringUtils.isNotEmpty(payScene)) {
            return builder.append("_").append(Math.abs(orderIdsString.hashCode())).append("_").append(payWay).append("_").append(payScene.toLowerCase()).toString();
        } else {
            return builder.append("_").append(Math.abs(orderIdsString.hashCode())).append("_").append(payWay.toLowerCase()).toString();
        }
    }

    /**
     * 获取订单核销单号
     *
     * @param uid
     * @return
     */
    public String getVerificationId(String uid) {
        StringBuilder paymentId = new StringBuilder();
        return paymentId.append(OperateCodeEnum.verification.getCode()).append(getGeneralId(uid)).toString();
    }

    /**
     * 获取用户开卡订单号
     *
     * @param uid
     * @return
     */
    public String getBindingBankCardId(String uid, OperateCodeEnum operateCodeEnum) {
        StringBuilder paymentId = new StringBuilder();
        return paymentId.append(operateCodeEnum.getCode()).append(getGeneralId(uid)).toString();
    }

    /**
     * 获取用户查询银行卡列表接口
     *
     * @param uid
     * @return
     */
    public String getQueryBankListId(String uid) {
        StringBuilder paymentId = new StringBuilder();
        return paymentId.append(OperateCodeEnum.QUERY_BANK_LIST.getCode()).append(getGeneralId(uid)).toString();
    }

    /**
     * 获取二维码支付单号
     *
     * @param uid
     * @return
     */
    public String getQrOrderId(String uid) {
//        long id = (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() << 12) | Math.abs(uid.hashCode()) | CommonUtil.getRandom(10, 10000000);
        return new StringBuilder().append(OperateCodeEnum.QR_ORDER.getCode()).append(getGeneralId(uid)).toString();
    }

    /**
     * 获取批次支付单号
     *
     * @param uid
     * @return
     */
    public String getBatchOrderId(String uid, Integer orderNum) {
//        long id = (LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() << 12) | Math.abs(uid.hashCode()) | CommonUtil.getRandom(10, 10000000);
        return new StringBuilder().append(OperateCodeEnum.BATCH_ORDER.getCode()).append(orderNum).append(getGeneralId(uid)).toString();
    }


    /**
     * 获取账单号
     *
     * @param orderId
     * @return
     */
    public String getBillNo(String orderId) {
        return new StringBuilder().append(OperateCodeEnum.BILL_NO.getCode()).append(getGeneralId(orderId)).toString();
    }

    /**
     * 获取通用规则id
     *
     * @param uid
     * @return
     */
    private StringBuilder getGeneralId(String uid) {
        StringBuilder id = new StringBuilder();

        int random = (int) ((Math.random() * 9 + 1) * 1000);
        String uidHashCode = String.valueOf(Math.abs(uid.hashCode() + random));
        uidHashCode = uidHashCode.substring(uidHashCode.length() >= 6 ? uidHashCode.length() - 6 : 0);

        id.append(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()).append(uidHashCode);
        return id;
    }
}
